package com.errol.batchprocessor.employeeaddition.advisorimpl.employee;

import com.baomidou.mybatisplus.mapper.Condition;
import com.renjia.dao.*;
import com.renjia.entity.CoreEmployeeCustomer;
import com.renjia.entity.CoreEmployeeCustomerTmp;
import com.renjia.entity.CoreEmployeeImportBatch;
import com.renjia.service.CoreEmployeeImportBatchService;
import com.renjia.service.generic.log.LogService;
import com.renjia.service.generic.middleware.batchprocessor.payload.AbstractBatchPayload;
import com.renjia.service.generic.support.customer.CustomerChannelSupportService;
import com.renjia.service.generic.support.employee.BankCardSupportService;
import com.renjia.service.generic.support.employee.EmployeeCustomerSupportService;
import com.renjia.service.generic.support.employee.EmployeeSupportService;
import com.renjia.service.integration.threeelements.ThreeElementsService;
import com.renjia.service.module.employee.AgreementSigningService;
import com.renjia.service.module.employeeaddition.advisorimpl.EmployeeAdvisorTypeEnum;
import com.renjia.service.module.employeeaddition.advisorimpl.common.EmployeeIdentityInfoModifier;
import com.renjia.service.module.employeeaddition.advisorimpl.common.ThreeElementForceHandler;
import com.renjia.service.module.employeeaddition.advisorimpl.employee.payload.EmployeeListBatchPayload;
import com.renjia.service.module.employeeaddition.advisorimpl.employee.payload.EmployeeListPiecePayload;
import com.renjia.service.module.employeeaddition.advisorimpl.employee.payload.EmployeeListSinglePayload;
import com.renjia.util.EpayCallingService;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Service;

/**
 * 人员列表编辑单条数据的扩展
 *
 * @author zhang xiao bin
 * @version v1.0
 * @date 2021/03/20
 * @since v5.0.6
 **/
@Service
public class EmployeeListManageAdvisorImpl extends EmployeeListAdvisorImpl
        implements
        ThreeElementForceHandler<Object, CoreEmployeeCustomerTmp, EmployeeListBatchPayload, EmployeeListPiecePayload, EmployeeListSinglePayload, EmployeeListManageAdvisorImpl>,
        EmployeeIdentityInfoModifier<Object, CoreEmployeeCustomerTmp, EmployeeListBatchPayload, EmployeeListPiecePayload, EmployeeListSinglePayload, EmployeeListManageAdvisorImpl> {

    public EmployeeListManageAdvisorImpl(CoreEmployeeImportBatchService coreEmployeeImportBatchService,
                                         CoreEmployeeMapper coreEmployeeMapper, CoreEmployeeCustomerMapper coreEmployeeCustomerMapper,
                                         CoreUserBankMapper coreUserBankMapper, CoreAgreementDetailMapper coreAgreementDetailMapper,
                                         ThreeElementsService threeElementsService, EmployeeSupportService employeeSupportService,
                                         EmployeeCustomerSupportService employeeCustomerSupportService, BankCardSupportService bankCardSupportService,
                                         CustomerChannelSupportService customerChannelSupportService, AgreementSigningService agreementSigningService,
                                         EpayCallingService epayCallingService, LogService logService, CoreEmployeeCustomerTmpMapper coreEmployeeCustomerTmpMapper) {
        super(coreEmployeeImportBatchService, coreEmployeeMapper, coreEmployeeCustomerMapper, coreUserBankMapper, coreAgreementDetailMapper,
                threeElementsService, employeeSupportService, employeeCustomerSupportService, bankCardSupportService, customerChannelSupportService,
                agreementSigningService, epayCallingService, logService, coreEmployeeCustomerTmpMapper);
    }

    @Override
    public boolean shallProcess(@NonNull AbstractBatchPayload<CoreEmployeeImportBatch, EmployeeAdvisorTypeEnum, Object, CoreEmployeeCustomerTmp> advisorPayload) {
        return EmployeeAdvisorTypeEnum.EMPLOYEE_LIST_MANAGE == advisorPayload.getAdvisorTypeEnum();
    }

    @Override
    public boolean doPrepareOnSingleStart(@NonNull EmployeeListSinglePayload singlePayload, @NonNull EmployeeListPiecePayload piecePayload, @NonNull EmployeeListBatchPayload batchPayload) {
        // 允许重复验证，除了单条数据也需要做的准备
        prepareForDuplicationCheckOfDocument(batchPayload);
        piecePayload.prepareValidator();
        return true;
    }

    @Override
    public boolean doExecuteOnSingleProcess(@NonNull EmployeeListSinglePayload singlePayload, @NonNull EmployeeListPiecePayload piecePayload, @NonNull EmployeeListBatchPayload batchPayload) {
        return super.doExecuteOnSingleProcess(singlePayload, piecePayload, batchPayload);
//        // remark4 用于传递新增或编辑的 employeeId，不会保存到 db
//        if (StringUtils.isEmpty(singlePayload.getSingleEntity().getRemark4())) {
//            // 新增，走添加的逻辑
//        } else {
//            // 编辑，走修改的逻辑
//            singlePayload.setCoreEmployee(coreEmployeeMapper.selectById(singlePayload.getSingleEntity().getRemark4()));
//            return doModifyEmployeeInfo(singlePayload, piecePayload, batchPayload, this);
//        }
    }

    @Override
    public boolean isBankCardModified(EmployeeListSinglePayload singlePayload) {
        // 人员列表编辑，se.id 是 employeeCustomer 的 id，检查是否编辑了手机号
        return coreEmployeeCustomerMapper.selectCount(Condition.<CoreEmployeeCustomer>wrapper()
                .eq("id", singlePayload.getSingleEntity().getId()).eq("bank_card", singlePayload.getBankCard())) == 0;
    }

    @Override
    protected boolean dealWithCoreEmployee(EmployeeListSinglePayload singlePayload, EmployeeListPiecePayload piecePayload, EmployeeListBatchPayload batchPayload) {
        if (super.dealWithCoreEmployee(singlePayload, piecePayload, batchPayload)) {
            return true;
        }
        // 新增用户时，三要素失败，且满足一定次数，生成待审核用户记录
        return forceReviewCoreEmployee(singlePayload, piecePayload, batchPayload, this);
    }

    @Override
    protected boolean dealWithCoreUserBank(EmployeeListSinglePayload singlePayload, EmployeeListPiecePayload piecePayload, EmployeeListBatchPayload batchPayload) {
        super.dealWithCoreUserBank(singlePayload, piecePayload, batchPayload);
        return forceReviewUserBank(singlePayload, piecePayload, batchPayload, this);
    }

    @Override
    protected void updateValidateSuccess(EmployeeListSinglePayload singlePayload, EmployeeListPiecePayload piecePayload, EmployeeListBatchPayload batchPayload) {
        // remark4 用于给 CoreEmployeeCustomerService#editEmployeeCustomer 方法传递 employeeId，但 se 仅存在于内存，无需保存到 db
        singlePayload.getSingleEntity().setRemark4(singlePayload.getCoreEmployee().getId());
        if (!singlePayload.getBankCard().equals(singlePayload.getCoreEmployeeCustomer().getBankCard())){
            singlePayload.getCoreEmployeeCustomer().setBankCard(singlePayload.getBankCard());
            coreEmployeeCustomerMapper.updateById(singlePayload.getCoreEmployeeCustomer());
        }
    }

    @Override
    protected void updateValidateFailed(EmployeeListSinglePayload singlePayload, EmployeeListPiecePayload piecePayload, EmployeeListBatchPayload batchPayload, String errMsg) {
        // 失败不保存
    }

    @Override
    public boolean doCleanUpOnBatchPost(@NonNull EmployeeListBatchPayload batchPayload) {
        return true;
    }
}
